package com.kingdee.eas.patch;

import java.io.*;

/**
 * IO工具类
 * Created by Niki on 2018/5/15 14:01
 */
public class IOUtils {
    protected static long copyLarge(InputStream input, OutputStream output) throws IOException {
        return copy(input, output, 1024);
    }

    protected static long copy(InputStream input, OutputStream output, int bufferSize) throws IOException {
        return copyLarge(input, output, new byte[bufferSize]);
    }

    protected static long copyLarge(InputStream input, OutputStream output, byte[] buffer) throws IOException {
        long count;
        int n;
        for (count = 0L; -1 != (n = input.read(buffer)); count += (long) n) {
            output.write(buffer, 0, n);
        }

        return count;
    }

    protected static void closeQuietly(InputStream input) {
        closeQuietly((Closeable) input);
    }

    protected static void closeQuietly(OutputStream output) {
        closeQuietly((Closeable) output);
    }

    private static void closeQuietly(Closeable closeable) {
        try {
            if (closeable != null) {
                closeable.close();
            }
        } catch (IOException var2) {
            ;
        }
    }

    public static byte[] getBytes(String filePath) throws PatchException {
        File javaFile = new File(filePath);
        InputStream inputStream = null;
        ByteArrayOutputStream outputStream = null;
        try {
            inputStream = new FileInputStream(javaFile);
            outputStream = new ByteArrayOutputStream();
            byte[] buffer = new byte[1024];
            int len = 0;
            while ((len = inputStream.read(buffer)) != -1) {
                outputStream.write(buffer, 0, len);
            }

            byte[] clazzInfo = outputStream.toByteArray();
            return clazzInfo;
        } catch (FileNotFoundException e) {
            throw new PatchException("没找到补丁文件：" + filePath);
        } catch (IOException e) {
            throw new PatchException("获取补丁文件：" + filePath + "字节数组异常！");
        } finally {
            closeQuietly(outputStream);
            closeQuietly(inputStream);
        }
    }
}
